#ifndef __SYS_H__
#define __SYS_H__
#include "pmu_rtc.h"
#include "pannel.h"
#include "pmu_cec.h"

//#define PMU_MCU_M3503   1
#define PMU_MCU_M3821   1
//#define PMU_MCU_DEBUG   1
#define PMU_CEC 1 //enable cec function
/* 
if you want to use cec pin as gpio ,
please close #define PMU_CEC 1 maro PMU_CEC
*/


typedef unsigned char      UINT8;
typedef unsigned short     UINT16;
typedef unsigned long      UINT32;
typedef char               INT8;
typedef short              INT16;
typedef long               INT32;

#define SUCCESS     0
#define ERROR      -1

#define  MCU_SYS_INT_STS 0xc024


/*
*	P===>>>0X50
*	M===>>>0X4D
*	U===>>>0X55
*/
#define MAILBOX_GET_EXIT_STANDBY_STATUS0  0xc200  //scratch register
#define MAILBOX_GET_EXIT_STANDBY_STATUS1  0xc201  //scratch register
#define MAILBOX_GET_EXIT_STANDBY_STATUS2 0xc202  //scratch register

#define  MAILBOX_GET_EXIT_STANDBY_STATUS 0xC209
#define  MAILBOX_GET_MONTH        0xC208
#define  MAILBOX_GET_DAY          0xC207
#define  MAILBOX_GET_HOUR         0xC206
#define  MAILBOX_GET_MIN          0xC205
#define  MAILBOX_GET_SEC          0xC204


#define  MAILBOX_SET_YEAR_H       0xc23B   //from CPU ->MCU
#define  MAILBOX_SET_YEAR_L       0xc23A

#define  MAILBOX_SET_PANEL_KEY_EN 0xc239
#define  MAILBOX_SET_MONTH        0xc238

#define  MAILBOX_SET_DAY          0xc237
#define  MAILBOX_SET_HOUR         0xc236
#define  MAILBOX_SET_MIN          0xc235
#define  MAILBOX_SET_SEC          0xc234
#define  MAILBOX_SET_POWERLOW3     0xc233
#define  MAILBOX_SET_POWERLOW2     0xc232
#define  MAILBOX_SET_POWERLOW1     0xc231
#define  MAILBOX_SET_POWERLOW0     0xc230


#define  MAILBOX_GET_YEAR_H       0xC20B   //from mcu->cpu
#define  MAILBOX_GET_YEAR_L       0xC20A

#define SYS_IOBASE		    0xC000
#define SYS_REG_ISR         (SYS_IOBASE+0x20)
#define SYS_REG_IPR         (SYS_IOBASE+0x21) 
#define SYS_REG_IER         (SYS_IOBASE+0x22) 



#define SYS_REG_RST			(SYS_IOBASE+0x90) 


/****************************************/
#define  MAILBOX_WAKE_MONTH       0xc23F   //from CPU ->MCU
#define  MAILBOX_WAKE_DAY         0xc23E  
#define  MAILBOX_WAKE_HOUR        0xc23D   
#define  MAILBOX_WAKE_MIN         0xc23C  
////////////////////


#define WRITE_BYTE(address,value)  (*(volatile unsigned char xdata *)(address)=value)
#define READ_BYTE(address)  (*(volatile unsigned char xdata *)(address))
#define PMU_WRITE_BYTE(address,value)  (*(volatile unsigned char xdata *)(address)=value)
#define PMU_READ_BYTE(address)  (*(volatile unsigned char xdata *)(address))

/*for gpio*/

#define  HAL_GPIO1_DO_REG       0xc05d   //output register 
#define  HAL_GPIO1_DI_REG       0xc05c   //Input Status Register
#define  HAL_GPIO1_DIR_REG      0xc05e   //Output control register
#define  HAL_GPIO1_EN           0xc05f  //Output control register


#define  HAL_GPIO_DIR_REG	    0xc056       //Output control register
#define  HAL_GPIO_DI_REG        0xc054    //Input Status Register
#define  HAL_GPIO_DO_REG		0xc055   //output register 
#define  HAL_GPIO_EN            0xc057     //GPIO ENABLE



#define  HAL_GPIO_O_DIR	    	1
#define  HAL_GPIO_I_DIR         0

#define  HAL_GPIO_SET_HI	      1
#define  HAL_GPIO_SER_LOW         0

/* 3503 has 3 pin can use dor gpio*/
#if  PMU_MCU_M3503
    #define XPMU_GPIO_0   0
    #define XPMU_GPIO_1   1
    #define XPMU_CEC      3
#else
// for gpio 0
    #define XPMU_GPIO_0   0
    #define XPMU_GPIO_1   1
    #define XPMU_GPIO_2   2
    #define XPMU_GPIO_3   3
    #define XPMU_GPIO_4   4
//for gpio1 
    #define XPMU_GPIO1_0   8
    #define XPMU1_CEC      10
#endif


//	#define  HAL_GPIO_WRITE(val)			(*(volatile UINT8 xdata*)HAL_GPIO_DO_REG  = (val)) //output register
//	#define  HAL_GPIO_READ()				(*(volatile UINT8 xdata*)HAL_GPIO_DI_REG)   //Input Status Register 
//	#define  HAL_GPIO_DIR_GET()			    (*(volatile UINT8 xdata*)HAL_GPIO_DIR_REG)  //dir control register
//	#define  HAL_GPIO_DIR_SET(val)		    (*(volatile UINT8 xdata*)HAL_GPIO_DIR_REG  = (val)) //Output control register
//	#define  HAL_GPIO_EN(val)		        (*(volatile UINT8 xdata*)HAL_GPIO_EN  = (val)) //gpio enable
//	
//	#define  HAL_GPIO1_WRITE(val)			(*(volatile UINT8 xdata*)HAL_GPIO1_DO_REG  = (val)) //output register
//	#define  HAL_GPIO1_READ()				(*(volatile UINT8 xdata*)HAL_GPIO1_DI_REG)   //Input Status Register 
//	#define  HAL_GPIO1_DIR_GET()			(*(volatile UINT8 xdata*)HAL_GPIO1_DIR_REG)  //dir control register
//	#define  HAL_GPIO1_DIR_SET(val)		    (*(volatile UINT8 xdata*)HAL_GPIO1_DIR_REG  = (val)) //Output control register
//	#define  HAL_GPIO1_EN(val)		        (*(volatile UINT8 xdata*)HAL_GPIO1_EN  = (val)) //gpio1 enable





#define NORMAL_STATUS 0
#define ENTER_STANDBY 1
#define SUCCESS       0
#define ERROR         -1

enum KEY_MODE
{
  NORMAL=0,
  STABDY
};

void init_externint2(void);
void init_externint3(void);
void init_externint4(void);
void init_externint5(void);


//main cpu enter standby
extern UINT8  g_standby_flag ;
// exit standby flag ,key,ir or time
extern UINT8  g_exit_standby_sts;

extern  void  pmu_init();
extern void   standby_init();
extern void   pannel_process(pRTC_TIMER rtc, enum SHOW_TYPE type);
extern void   ir_process(pRTC_TIMER rtc);
extern void   rtc_process(pRTC_TIMER rtc,pRTC_TIMER wakeup_rtc);
extern void ali_power_key_process(pRTC_TIMER rtc);
extern void debug_delay(UINT32 n);


extern void hal_mcu_gpio_en(pos);
extern void hal_mcu_gpio_disable_en();
extern UINT8 hal_gpio_bit_get(pos);
extern void hal_gpio_bit_set(pos,val);
extern void hal_gpio_bit_dir_set(pos,val);
extern void pmu_cec_process(pRTC_TIMER rtc);

/*
pmu parameter map, which is use for
commuicate for main cpu and mcu
*/

#define SHOW_TYPE_PAR  0x3fff
#define PANNEL_POWER_STATUS  0x3ffe
#define EXIT_STANDBY_TYPE 0x3ffd

#endif

